﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;


namespace RedditViewer
{
    public static class Utils
    {
        public static T GetSetting<T>(string Name, T DefaultValue)
        {
            object o = Properties.Settings.Default.Properties[Name];
            if (o == null)
                return DefaultValue;
            if (o is System.Configuration.SettingsProperty)
            {
                System.Configuration.SettingsProperty prop = o as System.Configuration.SettingsProperty;
                if (prop.DefaultValue == null)
                    return DefaultValue;
            }
            return (T)o;
        }
        public static void SetSetting(string Name, object Value)
        {
            object o = Properties.Settings.Default.Properties[Name];
            if (o == null)
                Properties.Settings.Default.Properties.Add(new System.Configuration.SettingsProperty(Name));

            Properties.Settings.Default.Properties[Name].DefaultValue = Value;
            Properties.Settings.Default.Save();
        }
        public static string ToAgoString(this TimeSpan s)
        {
            // 1.
            // Get time span elapsed since the date.
            

            // 2.
            // Get total number of days elapsed.
            int dayDiff = (int)s.TotalDays;

            // 3.
            // Get total number of seconds elapsed.
            int secDiff = (int)s.TotalSeconds;

            // 4.
            // Don't allow out of range values.
            if (dayDiff < 0)
            {
                return null;
            }

            // 5.
            // Handle same-day times.
            if (dayDiff == 0)
            {
                // A.
                // Less than one minute ago.
                if (secDiff < 60)
                {
                    return "just now";
                }
                // B.
                // Less than 2 minutes ago.
                if (secDiff < 120)
                {
                    return "1 minute ago";
                }
                // C.
                // Less than one hour ago.
                if (secDiff < 3600)
                {
                    return string.Format("{0} minutes ago",
                        Math.Floor((double)secDiff / 60));
                }
                // D.
                // Less than 2 hours ago.
                if (secDiff < 7200)
                {
                    return "1 hour ago";
                }
                // E.
                // Less than one day ago.
                if (secDiff < 86400)
                {
                    return string.Format("{0} hours ago",
                        Math.Floor((double)secDiff / 3600));
                }
            }
            // 6.
            // Handle previous days.
            if (dayDiff == 1)
            {
                return "yesterday";
            }
            if (dayDiff < 7)
            {
                return string.Format("{0} days ago",
                dayDiff);
            }
            else
            {
                return string.Format("{0} weeks ago",
                Math.Ceiling((double)dayDiff / 7));
            }
            return null;
        }


         

    const int SPI_SETDESKWALLPAPER = 20;
    const int SPIF_UPDATEINIFILE = 0x01;
    const int SPIF_SENDWININICHANGE = 0x02;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

    public enum WallpaperStyle : int
    {
        Tiled,
        Centered,
        Stretched
    }

    public static void SetWallpaper(BitmapImage img, WallpaperStyle style)
    {


        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        Guid photoID = System.Guid.NewGuid();
        string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.jbg");
        encoder.Frames.Add(BitmapFrame.Create((BitmapImage)img));

        using (var filestream = new FileStream(tempPath, FileMode.Create))
            encoder.Save(filestream);

        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
        if (style == WallpaperStyle.Stretched)
        {
            key.SetValue(@"WallpaperStyle", 2.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == WallpaperStyle.Centered)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 0.ToString());
        }

        if (style == WallpaperStyle.Tiled)
        {
            key.SetValue(@"WallpaperStyle", 1.ToString());
            key.SetValue(@"TileWallpaper", 1.ToString());
        }

        SystemParametersInfo(SPI_SETDESKWALLPAPER,
            0,
            tempPath,
            SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    }

    }
}
